package com.kornero.auto.view;

import com.kornero.auto.util.view.LayoutManager;
import com.kornero.auto.util.view.SWTFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class MainForm {

    private static final Logger logger = LoggerFactory.getLogger(MainForm.class);

    private final Display display;
    private final Shell shell;

    private final PropertiesPanel propertiesPanel;
    private final ITablePanel tablePanel;

    public MainForm() {
        this.display = new Display();
        this.shell = new Shell(display);
        shell.setText("Checker for Auto sites by Kornero.");
        shell.setLayout(LayoutManager.getGridLayout());
        shell.setLayoutData(LayoutManager.getLayoutDataFillAll());
        shell.setMaximized(true);

        Composite main = new Composite(shell, SWT.BORDER);
        main.setLayout(LayoutManager.getGridOneColumnLayout());
        main.setLayoutData(LayoutManager.getLayoutDataFillAll());

        propertiesPanel = new PropertiesPanel(main);
        tablePanel = SWTFactory.createObject(new TablePanelImpl(main), ITablePanel.class);

        main.pack();
        shell.setMinimumSize(1050, 300);
        shell.setSize(1050, 480);
        shell.pack();
    }

    public void show() {
        final Thread me = Thread.currentThread();

        shell.open();
        while (!shell.isDisposed() && !me.isInterrupted()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        if (!display.isDisposed()) {
            display.dispose();
        }
    }

    public PropertiesPanel getPropertiesPanel() {
        return propertiesPanel;
    }

    public ITablePanel getTablePanel() {
        return tablePanel;
    }

    public void execute(final Runnable runnable) {
        display.asyncExec(runnable);
    }
}
